package com.bbcare.treat.plat.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.SeqUtil;
import com.bbcare.comm.ToolUtils;
import com.bbcare.treat.plat.service.IOfflineTreatService;

/**
 * @name: 离线诊疗系统相关处理类
 * @author: DemonLee
 * @createTime: 2017.11.15
 * @description: 离线门诊诊疗相关操作
 * @modify:
 *
 */
@Controller
@RequestMapping("/service/offline")
public class OfflineTreatController {
    private final Log logger = LogFactory.getLog(OfflineTreatController.class);

    @Autowired
    private IOfflineTreatService offlineTreatService;

    // 查询离线系统菜单
    @ResponseBody
    @RequestMapping("/querySystemOfflineMenu")
    public ResultModel<Map<String, Object>> listSystemOfflineMenu(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in listSystemOfflineMenu----arg==" + arg);

        // 基础校验
        ToolUtils.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        Map<String, Object> mapIn = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 入参校验
        Map<String, Object> chkVar = new HashMap<String, Object>();
        chkVar.put("accountId", "账户id");
        ToolUtils.checkInputInfo(Constants.INF_INPUTPARAM_COMMONFIELD, mapIn, chkVar);

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        mapIn.put("tenantId", sessionMap.get("tenantId"));

        Map<String, Object> retMap = offlineTreatService.listOfflineMenu(mapIn);

        return new ResultModel<Map<String, Object>>(retMap);
    }

    // 离线任务内容提交
    @ResponseBody
    @RequestMapping("/submitOfflineTaskCont")
    public ResultModel<Map<String, Object>> submitOfflineTaskCont(@RequestBody String arg, HttpServletRequest request)
            throws Exception {
        logger.warn("in submitOfflineTaskCont----arg==" + arg);

        // 基础校验
        ToolUtils.checkInputRoot(arg);

        JSONObject jsonArg = JSON.parseObject(arg);
        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

        // 入参校验
        JSONObject jsonTask = jsonData.getJSONObject("task");
        Map<String, Object> chkVar = new HashMap<String, Object>();
        chkVar.put("taskId", "任务模板id");
        ToolUtils.checkInputInfo("task", jsonTask, chkVar);

        chkVar.clear();
        JSONObject jsonOpInfo = jsonData.getJSONObject("oprInfo");
        chkVar.put("schemeBusiType", "方案类型");
        chkVar.put("opCode", "操作编码");
        chkVar.put("followQueueId", "队列id");
        ToolUtils.checkInputInfo("oprInfo", jsonOpInfo, chkVar);

        chkVar.clear();
        Map<String, Object> questiones = jsonData.getJSONObject("questiones");
        // chkVar.put(Constants.QUESTION_ID_FOR_PHONENO, "手机号码");
        chkVar.put(Constants.QUESTION_ID_FOR_PATIENTNAME, "患者姓名");
        chkVar.put(Constants.QUESTION_ID_FOR_BIRTHDAY, "出生日期");
        ToolUtils.checkInputInfo("questiones", questiones, chkVar);

        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 方案业务类型
        String schemeBusiType = jsonOpInfo.getString("schemeBusiType");
        int schemeBusiTypes = Integer.parseInt(schemeBusiType);

        // 登录信息，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
        String authorName = (String) sessionMap.get("authorName");// 登录账号医生姓名
        String updateAuname = jsonTask.getString("updateAuname");
        if (StringUtils.isBlank(updateAuname)) {
            updateAuname = authorName;
        }

        String currTime = jsonOpInfo.getString("opTime");// 入参中有操作时间，则使用入参中的时间
        if (StringUtils.isBlank(currTime)) {
            currTime = DateUtil.getTimeString(DateUtil.getSysDateA());// 创建时间
        }
        paramMap.put("authorName", authorName);
        paramMap.put("deptId", sessionMap.get("deptId"));
        paramMap.put("taskId", jsonTask.getString("ptTaskId"));// 任务实例id
        paramMap.put("task", jsonTask.getString("taskId"));// 任务id
        paramMap.put("contentData", questiones);
        paramMap.put("updateTime", currTime);
        paramMap.put("updateAccept", SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));
        paramMap.put("authorId", sessionMap.get("authorId"));
        paramMap.put("tenantId", sessionMap.get("tenantId"));
        paramMap.put("opCode", jsonOpInfo.getString("opCode"));
        paramMap.put("queueId", jsonOpInfo.getString("followQueueId"));
        paramMap.put("opNote", jsonOpInfo.getString("opNote"));
        paramMap.put("opType", jsonOpInfo.getString("opType"));
        paramMap.put("updateAuname", updateAuname);
        paramMap.put("finishDate", jsonTask.getString("finishDate"));
        paramMap.put("type", Constants.QUESTIONNAIRE_TYPE_0);// 随访
        paramMap.put("schemeBusiType", schemeBusiTypes);

        String taskNote = jsonTask.getString("taskNote");
        if (StringUtils.isBlank(taskNote)) {
            taskNote = (String) questiones.get(Constants.QUESTION_ID_FOR_TR_TASK_CHECK_NOTE);
        }
        String nextDate = jsonTask.getString("nextDate");
        if (StringUtils.isBlank(nextDate)) {
            nextDate = (String) questiones.get(Constants.QUESTION_ID_FOR_TR_TASK_NEXT_CHECK_DATE);
        }
        String nextAddress = jsonTask.getString("nextAddress");
        if (StringUtils.isBlank(nextAddress)) {
            nextAddress = (String) questiones.get(Constants.QUESTION_ID_FOR_TR_TASK_NEXT_CHECK_ADDRESS);
        }
        paramMap.put("taskNote", taskNote);
        paramMap.put("nextDate", nextDate);
        paramMap.put("nextAddress", nextAddress);

        Map<String, Object> retMap = offlineTreatService.commitOfflinePatientQueue(paramMap);

        return new ResultModel<Map<String, Object>>(retMap);
    }
}
